﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using System.Reflection;
using CRUD_Layer.General;

namespace CRUD_Layer.Session
{
    public class SessionManager
    {
        private static ISessionFactory SessionFactory;

        public static ISessionFactory CreateSessionFactory(string ConnectionString, string AssemblyName)
        {
            SessionFactory = Fluently.Configure()
              .Database(
                MsSqlConfiguration.MsSql2008.ConnectionString(ConnectionString)
              )
              .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load(AssemblyName)).Conventions.Add<StringColumnLengthConvention>())
              .ExposeConfiguration(c => c.Properties.Add("connection.isolation", "ReadCommitted"))
              .ExposeConfiguration(c => c.Properties.Add("hbm2ddl.keywords", "none"))
              .ExposeConfiguration(c => c.Properties.Add("generate_statistics", "true"))
              .BuildSessionFactory();

            return SessionFactory;
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}
